package code;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;

public class Test {

	/**
	 * @param args
	 */
	
	{System.out.print("1");}
	static{System.out.print("2");}
	
	public Test(int i) {
		System.out.println("Constructor");
	}
	
	public static void precedence() {
		int a=1;
		int b=1;
		int c=++a + b++;
		System.out.println(c);
		
		c+=b++ + magic() + a++;
		System.out.println(c);
	}
	
	public static int magic() {
		return 42;
	}
	
	public static long recFact(long r) {
		if (r==1)
			return 1;
		return r*recFact(r-1);
	}
	
	public static int fib(int r) {
		if (r==1)
			return 0;
		if (r==2)
			return 1;
		return fib(r-1)+fib(r-2);
	}
	
	public static BigInteger factBig(int r) {
		BigInteger n=BigInteger.ONE;
		for (int i=1; i<r; i++) {
			n=n.multiply(BigInteger.valueOf(i));
		}
		return n;
	}
	
	public static int fib2(int r) {
		if (r==1)
			return 0;
		if (r==2 || r==3)
			return 1;
		int prev1=0;
		int prev2=1;
		int sum=1;
		for (int i=4; i<=r; i++) {
			prev1=sum;
			sum=prev1+prev2;
			prev2=prev1;
		}
		return sum;
	}
	
	public static void magic2() {
		float value=0.2F;
		
		if (value==0.2) {
			doStuff();
		} else {
			System.out.println("No Stuff");
		}
		
		System.out.println(value);
	}
	
	private static void doStuff() {
		System.out.println("Stuff");
	}
	
	public static void modifyString(String s) {
		s+="world!";
	}
	
	public static String reverseString(String s) {
		String newString="";
		for (int i=s.length()-1; i>=0; i--) {
			newString+=s.charAt(i);
		}
		return newString;
	}
	
	public static String reverseString2(String s) {
		/*StringBuilder sb=new StringBuilder("");
		for (int i=s.length()-1; i>=0; i--) {
			sb.append(s.charAt(i));
		}
		return new String(sb);*/
		/*StringBuilder sb=new StringBuilder(s);
		for (int i=0; i<s.length()/2; i++) {
			char temp=sb.charAt(i);
			sb.insert(s.length()-i, temp);
			sb.insert(i, sb.charAt(i));
		}
		return new String(sb);*/
		char [] arrC=new char[s.length()];
		s.getChars(0, s.length(), arrC, 0);
		for (int i=0; i<s.length()/2; i++) {
			char temp=arrC[i];
			arrC[i]=arrC[s.length()-i-1];
			arrC[s.length()-i-1]=temp;
		}
		return new String(arrC);
	}
	
	public static <G extends Number> byte asByte(G num) {
		long n=num.longValue();
		if (n>=-128 && n<=127) 
			return (byte) n;
		else 
			return 0;
	}
	
	public static int getArgCount(Integer... args) {
		if (args.length==0)
			System.out.println("There is not args");
		else {
			for (int i: args)
				System.out.println(i);
		}
		return args.length;
	}
	
	public static void writeFile() {
		File f=new File("d:/test.txt");
		FileWriter fw;
		try {
			fw = new FileWriter(f);
			fw.write("New string");
			fw.close();
			System.out.println("Ok");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void sort(int [] arr) {
		boolean flag=true;
		int count=0;
		while (flag || count<arr.length) {
			flag=false;
			for (int i=0; i<arr.length-1-count; i++) {
				if (arr[i+1]<arr[i]) {
					int temp=arr[i];
					arr[i]=arr[i+1];
					arr[i+1]=temp;
					flag=true;
				}
			}
			count++;
		}
	}
	public static void main(String[] args) {
		
		
		byte b = 12;
		short s = 258;
		float f = 1.2f;
		int i = 0;
		System.out.println(i);
		i = b;
		b = (byte) i;

		byte b3 = 3;
		byte b4 = 4;
		//byte b5=b3+b4;
		byte b5 = 1;
		b5 += b3 + b4;
		b5 += 256;
		System.out.println(b5);
		byte b6 = 3 + 4;
		int y = -5;
		System.out.println(-y);

		precedence();
		
		System.out.println(recFact(20));
		System.out.println(fib(37));
		System.out.println(fib2(37));
		System.out.println(factBig(100));
		
		magic2();
		System.out.println(0.7-0.4);
		
		String s2="Hello";
		
		modifyString(s2);
		System.out.println(s2);
		String empty=new String();
		System.out.println(empty.equals(""));
		
		StringBuilder sb=new StringBuilder("Hello");
		sb.reverse();
		System.out.println(sb);
		
		String sr="Hello";
		System.out.println(reverseString(sr));
		
		System.out.println(reverseString2("My string"));
		
		System.out.println(asByte(new Float("7.f")));
		
		
		getArgCount(1, 4, 3, 7);
		getArgCount();
		
		//Test t=new Test();
		int i5='1';
		
		writeFile();
		int sdf=Integer.parseInt("2");
		String sfsdf=Integer.toString(5);
		System.out.println(sdf+sfsdf);
		
		int [] arr={2, 5, 1, 3, 9, 4, 12, 6, 19, 2};
		sort(arr);
		for (int j = 0; j < arr.length; j++) {
			System.out.print(arr[j]+" ");
		}
	}

}

